package com.neusoft.EYSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.EYSystem.dao.CheckinRecordDAO;
import com.neusoft.EYSystem.po.*;
import com.neusoft.EYSystem.service.CheckinRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/checkinRecord")
public class CheckinRecordController {

    @Autowired
    private CheckinRecordService checkinRecordService;

    @Autowired
    private CheckinRecordDAO checkinRecordDAO;

    @GetMapping("/listNum")
    public ResponseBean<Map<String, Integer>> getCheckinRecordNum() {
        ResponseBean<Map<String, Integer>> response = new ResponseBean<>();
        try {
            Map<String, Integer> count = new HashMap<>();
            // 查询一周内每天的数量
            for (int i = 0; i < 7; i++) {
                String dateKey = java.time.LocalDate.now().minusDays(i).toString();
                QueryWrapper<CheckinRecord> qw = new QueryWrapper<>();
                qw.eq("is_visible", true);
                qw.eq("checkin_date", dateKey);
                Long num = checkinRecordDAO.selectCount(qw);
                count.put(dateKey, num.intValue());
            }
            response.setStatus(200);
            response.setMsg("记录数量查询成功");
            response.setData(count);
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("记录数量查询失败: " + e.getMessage());
        }
        return response;
    }



    @GetMapping("/getCheckinRecord")
    public ResponseBean<CheckinRecord> getCheckinRecord(Integer recordId) {
        ResponseBean<CheckinRecord> response = new ResponseBean<>();
        try {
            CheckinRecord checkinRecord = checkinRecordDAO.selectById(recordId);
            if (checkinRecord != null) {
                response.setStatus(200);
                response.setMsg("记录查询成功");
                response.setData(checkinRecord);
            } else {
                response.setStatus(404);
                response.setMsg("记录不存在");
            }
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("记录查询失败: " + e.getMessage());
        }
        return response;
    }

    @PostMapping("/updateEndDate")
    public ResponseBean<Integer> updateCheckinRecordEndDate(Client client) {
        return checkinRecordService.updateCheckinRecordEndDate(client);
    }

    @GetMapping("/checkinPage")
    public PageResponseBean<List<ClientCheckinRecord>> checkinPage(String name, String checkinDateStart, String checkinDateEnd, String status, Long page, Long size) {
        return checkinRecordService.checkinPage(name, checkinDateStart, checkinDateEnd, status, page, size);
    }

    @PostMapping("/delete")
    public ResponseBean<Integer> deleteCheckinRecordLogic(Integer recordId){
        return checkinRecordService.deleteCheckinRecordLogic(recordId);
    }

    @PostMapping("/update")
    public ResponseBean<Integer> updateCheckinRecord(CheckinRecord checkinRecord) {
        ResponseBean<Integer> response = new ResponseBean<>();
        try {
            int result = checkinRecordDAO.updateById(checkinRecord);
            if (result > 0) {
                response.setStatus(200);
                response.setMsg("记录更新成功");
                response.setData(result);
            } else {
                response.setStatus(400);
                response.setMsg("记录更新失败，所需数据不完整或不存在");
            }
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("记录更新失败: " + e.getMessage());
        }
        return response;
    }

}
